#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn], c[maxn], A[maxn];
char d[31][maxn];
int K = 0, n, m;
int main(){
	freopen("arena.in", "r", stdin);
	freopen("arena.out", "w", stdout);
	ios :: sync_with_stdio(false);
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		cin >> A[i];
	for (int i = 1; i <= m; i++)
		cin >> c[i];
	while ((1 << K) < n) K++;
	for (int i = 1; i <= K; i++)
		for (int j = 1; j <= (1 << (K - i)); j++)
			cin >> d[i][j];
	int T;
	cin >> T;
	for (int t = 1; t <= T; t++){
		int x[4];
		cin >> x[0] >> x[1] >> x[2] >> x[3];
		for (int i = 1; i <= n; i++)
			a[i] = A[i] ^ x[i % 4];
		int ans = 1;
		for (int i = 2; i <= c[t]; i++)
			ans = ans ^ i;
		cout << ans << endl;
	}
	return 0;
}
